import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    component: HomeView,
    redirect: '/dashboard',  // 添加重定向
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        meta:{
          title:"智慧校园图书管理系统-首页"
        },    
        component: () => import('@/views/manager/Home.vue')
      },
      {
        path: 'books',
        name: 'books',
        meta:{
          title:"智慧校园图书管理系统-图书管理"
        },
        component: () => import('@/views/manager/Books.vue')
      },
      {
        path: 'readers',
        name: 'readers',
        meta:{
          title:"智慧校园图书管理系统-学生管理"
        },
        component: () => import('@/views/manager/Readers.vue')
      },
      {
        path: 'borrow',
        name: 'borrow',
        meta: {
          title: '智慧校园图书管理系统-借阅管理',
          requiresAuth: true
        },
        component: () => import('@/views/manager/Borrow.vue')
      },
      {
        path: '/borrow-orders',
        name: 'BorrowOrders',
        component: () => import('@/views/manager/BorrowOrders.vue'),
        meta: {
          title: '智慧校园图书管理系统-订单管理',
          requiresAuth: true
        }
      },
      {
        path: 'statistics',
        name: 'statistics',
        meta:{
          title:"智慧校园图书管理系统-数据可视化"
        },
        component: () => import('@/views/manager/Statistics.vue')
      },
      // ... 其他子路由
    ]
  },
  {
    path: '/login',
    name: 'LoginView',
    meta:{
      title:"智慧校园图书管理系统-后台登录"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/LoginView.vue')
  },
  {
    path: '/userborrow',
    name: 'UserBorrowView',
    meta:{
      title:"智慧校园图书管理系统-借阅系统"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/UserBorrowView.vue')
  },
  
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

//路由验证登录
router.beforeEach((to, from, next) => {
  //获取token
  const token = localStorage.getItem('token')
  //判断是否需要登录
  if (to.path === '/login'||to.path === '/userborrow') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
